package com.hz.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hz.mapper.ArticleMapper;
import com.hz.pojo.Article;
import com.hz.pojo.PageBean;
import com.hz.service.ArticleService;
import com.hz.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int addArticle(Article article) {

        Map<String,Object> map = ThreadLocalUtil.get();

        Integer id = (Integer) map.get("id");

        article.setCreateUser(id);

        return articleMapper.insert(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        PageBean<Article> pb = new PageBean<>();

        PageHelper.startPage(pageNum,pageSize);
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");

        List<Article> articles = articleMapper.list(categoryId,state,id);

        Page<Article> pg = (Page<Article>) articles;

        pb.setTotal(pg.getTotal());
        pb.setItems(pg.getResult());
        return pb;

    }

    @Override
    public int updateArticle(Article article) {
        return articleMapper.update(article);
    }

    @Override
    public int deleteArticle(Integer id) {
        return articleMapper.delete(id);
    }

    @Override
    public Article getDetail(Integer id) {
        return articleMapper.selectById(id);
    }
}
